﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ZeroBug.PersonalityEvaluation
{
    public partial class LoginViewModel : ObservableObject
    {
        [ObservableProperty]
        bool isAdminLogin = false;
        [ObservableProperty]
        bool isShowPasswordBox = false;
        [ObservableProperty]
        string userType = "测评人员";
        [ObservableProperty]
        string userInfoPlacement = "请输入姓名";
        [ObservableProperty]
        string actionInfoPlacement = "开始测评";
        [ObservableProperty]
        string name="";
        [ObservableProperty]
        string passwordString="";
        partial void OnIsAdminLoginChanged(bool oldValue, bool newValue)
        {
            if (newValue)
            {
                UserType = "管理人员";
                IsShowPasswordBox = true;
                UserInfoPlacement = "请输入用户名";
                ActionInfoPlacement = "登    录";
                Name = "";
                PasswordString = "";
            }
            else
            {
                UserType = "测评人员";
                IsShowPasswordBox = false;
                UserInfoPlacement = "请输入姓名";
                ActionInfoPlacement = "开始测评";
                Name = "";
                PasswordString = "";
            }
        }
        [RelayCommand]
        void PasswordChanged(PasswordBox box)
        {
            PasswordString = box.Password;
        }
        [RelayCommand]
        void Login()
        {
            App.Current.Services.GetService<MainWindowViewModel>()!.IsAdminLogin = IsAdminLogin;

            Window? mainWindow = App.Current.Services.GetService<MainWindow>();
            Window? loginWindow = App.Current.Services.GetService<LoginView>();
            if (mainWindow != null && loginWindow != null)
            {
                if (!isAdminLogin)
                {
                    Person tp = new Person()
                    {
                        Name = Name,
                        Score = new ATCESore(),
                    };
                    App.Current.Services.GetService<EvaluationViewModel>().TestPerson = tp;
                    loginWindow.Close();
                    mainWindow.ShowDialog();
                }
                else
                {
                    var databaseHelper = App.Current.Services.GetService<IDatabase>();
                    DataTable dt = databaseHelper.Select($"select * from Admin where Name='{Name}';", out string errInfo);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["Password"].ToString().Equals(PasswordString))
                        {
                            loginWindow.Close();
                            mainWindow.ShowDialog();
                            return;
                        }
                    }
                    MessageBox.Show("用户名或密码错误!");
                }
            }
        }

        [RelayCommand]
        void Register()
        {
            var databaseHelper = App.Current.Services.GetService<IDatabase>();
            Dictionary<string, object> data = new Dictionary<string, object>();
            data.Add("Name", Name);
            data.Add("Password", PasswordString);
            databaseHelper.InsertOneRecord("Admin", data);
        }
    }
}
